package com.vhly.ddxdecompiler.dexreader.file.mixeditem;

import com.vhly.ddxdecompiler.dexreader.file.DexFile;
import com.vhly.ddxdecompiler.dexreader.file.FieldIdsSection;
import com.vhly.ddxdecompiler.dexreader.file.constant.CstConstant;
import com.vhly.ddxdecompiler.dexreader.io.DexInputStream;

import java.io.ByteArrayInputStream;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: vhly
 * Date: 2009-10-21
 * Time: 10:24:17
 *
 * @author vhly[FR]
 */
public class EncodeField implements EncodeMember {

    private DexFile dex;

    int fieldIdIndex;
    int accessFlags;
    int diff;

    FieldIdItem field;

    CstConstant constant;

    public EncodeField(DexFile dexFile) {
        dex = dexFile;
    }

    /**
     * 读取编码后的数据
     *
     * @param din 数据流
     * @throws java.io.IOException 异常
     */
    public void readEncode(DexInputStream din) throws IOException {
        diff = din.readUnsignedLeb128(); // 此处读取到的只是 diff ，还需要 外部 进行 + lastIndex处理
        accessFlags = din.readUnsignedLeb128();
    }

    /**
     * 重新定位
     *
     * @param bin    数据流
     * @param offset 起始偏移
     * @throws java.io.IOException 异常
     */
    public void reloc(ByteArrayInputStream bin, int offset) throws IOException {
        // TODO 此方法必须在 readEncode 并且 计算出 index 之后执行
        FieldIdsSection fieldIds = dex.getFieldIds();
        field = fieldIds.indexOf(fieldIdIndex);
    }
}
